package mlp.game.cherries;

import java.util.ArrayList;

public class CherrySpawner {

	private Belt belt;
	
	float spawnDistance = 0;
	float spawnDistanceModifier = 0; //0.00225f;
	float minSpawnDistance = 80;
	float lastSpawn = 0;
	float currentDistance = 0;
	
	Cherry[] basket;
	
	public CherrySpawner(Belt belt, Cherry[] basket) {
		this.belt = belt;
		this.basket = basket;
		this.spawnDistance = CherryEngine.dip(200f);
	}
	
	public void update(long gameTime){
		currentDistance += CherryEngine.GAME_SPEED;
		if(spawnDistance > minSpawnDistance)
			spawnDistance -= spawnDistanceModifier;
	}
	
	public void spawn(ArrayList<Cherry> list){
		if(lastSpawn == 0){
			list.add(createCherry(false));
			list.add(createCherry(true));
			lastSpawn = 1;
		}else if(currentDistance > lastSpawn + spawnDistance){
			lastSpawn = lastSpawn + spawnDistance;
			list.add(createCherry(false));
			list.add(createCherry(true));
		}
	}
	
	private Cherry createCherry(boolean offsetX){
		Cherry base = basket[(int) (Math.random() * basket.length)];
		Cherry c = new Cherry(base.getSprite(), base.getColor());
		float x = belt.getFrame().left - c.getWidth();
		if(offsetX)
			x -= c.getWidth() * 2.1f;
		c.moveTo(x,
				(float) ((Math.random() * (belt.getFrame().bottom - belt.getFrame().top - c.getHeight())) + belt.getFrame().top));
		return c;
	}
	
}
